/**
 * Copyright (C) 2009 Noel Ang
 *
 * This file is part of Samsara.
 *
 * Samsara is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Samsara is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Samsara.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package com.lunarshades.samsara;

import java.awt.Font;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.lunarshades.samsara.ui.SamsaraFrame;
import com.lunarshades.samsara.ui.fonts.Fonts;

/**
 * The Application.
 *
 * @author Noel Ang, sparta.variable@gmail.com
 */
public class Samsara {

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        overrideFonts();

        System.setProperty("swing.aatext", "true");
        System.setProperty("awt.useSystemAAFontSettings", "on");
        final SamsaraFrame app = new SamsaraFrame();
        app.pack();
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                app.setVisible(true);
            }
        }
        );
    } catch (UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

private static void overrideFonts() {
    UIDefaults uiDefaults = UIManager.getDefaults();

    // Find all font resources
    Collection<String> fontKeys = new ArrayList<String>();
    Enumeration<Object> keys = uiDefaults.keys();
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        if (key instanceof String) {
            String value = key.toString();
            if (value.contains(".font")) {
                fontKeys.add(String.valueOf(key));
            }
        }
    }

    // Replace font
    for (String key : fontKeys) {
        Font currentFont = uiDefaults.getFont(key);
        if (currentFont != null) {
            int currentFontSize = currentFont.getSize();
            Font newFont;
            switch (currentFont.getStyle()) {
            case Font.PLAIN:
            case Font.ITALIC:
                newFont = Fonts.getPlainFont();
                break;
            case Font.BOLD:
            case Font.BOLD+Font.ITALIC:
                newFont = Fonts.getBoldFont();
                break;
            default:
                newFont = null;
            }
            if (newFont != null) {
                newFont = newFont.deriveFont(currentFont.getStyle(), Math.max(
                        currentFontSize,
                        Math.max(FONT_MIN_SIZE, currentFontSize)
                )
                );
                uiDefaults.put(key, newFont);
            }
        }
    }
}

private static final float FONT_MIN_SIZE = 13F;
}
